package com.nexen.merchant.warrantyCard.controller;

import com.alibaba.fastjson2.JSON;
import com.nexen.common.annotation.Log;
import com.nexen.common.core.controller.BaseController;
import com.nexen.common.core.domain.AjaxResult;
import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.core.domain.model.LoginUser;
import com.nexen.common.core.page.TableDataInfo;
import com.nexen.common.enums.BusinessType;
import com.nexen.common.utils.SecurityUtils;
import com.nexen.common.utils.StringUtils;
import com.nexen.common.utils.poi.ExcelUtil;
import com.nexen.merchant.warrantyCard.domain.NexenWarrantycard;
import com.nexen.merchant.warrantyCard.service.INexenWarrantycardService;
import com.nexen.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 质保卡Controller
 * 
 * @author xiao
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/merchant/warrantyCard")
public class NexenWarrantycardController extends BaseController
{
    @Autowired
    private INexenWarrantycardService nexenWarrantycardService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询质保卡列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexenWarrantycard nexenWarrantycard)
    {
        startPage();
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                String cityIds = currentUser.getCityIds();
                if (StringUtils.isNotBlank(cityIds)){
                    List<String> cityList = Arrays.asList(cityIds.split(";"));
                    if (cityList != null && cityList.size() > 0) {
                        nexenWarrantycard.setCityList(cityList);
                    }
                }
            }
        }
        
        List<NexenWarrantycard> list = nexenWarrantycardService.selectNexenWarrantycardList(nexenWarrantycard);
        return getDataTable(list);
    }

    /**
     * 导出质保卡列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:export')")
    @Log(title = "质保卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexenWarrantycard nexenWarrantycard)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                String cityIds = currentUser.getCityIds();
                if (StringUtils.isNotBlank(cityIds)){
                    List<String> cityList = Arrays.asList(cityIds.split(";"));
                    if (cityList != null && cityList.size() > 0) {
                        nexenWarrantycard.setCityList(cityList);
                    }
                }
            }
        }

        List<NexenWarrantycard> list = nexenWarrantycardService.selectNexenWarrantycardList(nexenWarrantycard);
        ExcelUtil<NexenWarrantycard> util = new ExcelUtil<NexenWarrantycard>(NexenWarrantycard.class);
        util.exportExcel(response, list, "质保卡数据");
    }

    /**
     * 获取质保卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(nexenWarrantycardService.selectNexenWarrantycardById(Id));
    }

    /**
     * 新增质保卡
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:add')")
    @Log(title = "质保卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexenWarrantycard nexenWarrantycard)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenWarrantycard.setCreateBy(sysUser.getUserName());
        }
        return toAjax(nexenWarrantycardService.insertNexenWarrantycard(nexenWarrantycard));
    }

    /**
     * 修改质保卡
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:edit')")
    @Log(title = "质保卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexenWarrantycard nexenWarrantycard)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenWarrantycard.setUpdateBy(sysUser.getNickName());
        }
        return toAjax(nexenWarrantycardService.updateNexenWarrantycard(nexenWarrantycard));
    }

    /**
     * 审核质保卡
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:edit')")
    @Log(title = "审核质保卡", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult editAuditStatus(@RequestBody NexenWarrantycard nexenWarrantycard)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenWarrantycard.setAuditBy(sysUser.getNickName());
            nexenWarrantycard.setUpdateBy(sysUser.getNickName());
        }

        return toAjax(nexenWarrantycardService.updateAuditStatus(nexenWarrantycard));
    }

    /**
     * 删除质保卡
     */
    @PreAuthorize("@ss.hasPermi('merchant:warrantyCard:remove')")
    @Log(title = "质保卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(nexenWarrantycardService.deleteNexenWarrantycardByIds(Ids));
    }

}
